//
//  PGViewCell.m
//  PhotographicGuide
//
//  Created by indream on 13-2-9.
//  Copyright (c) 2013年 indream. All rights reserved.
//

#import "PGViewCell.h"
#import "PGArticleManager.h"
#import "PGLocalFileManager.h"

static NSString* NOTIFICATION_IMAGE_LOADED = @"__NOTIFICATION_IMAGE_LOADED__";
static NSString* NOTIFICATION_ICON_LOADED = @"__NOTIFICATION_ICON_LOADED__";

@implementation PGViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark - public 

-(void)loadImage:(NSString*)url{
    if([url componentsSeparatedByString:@"http://"].count<2){
        return;
    }
    self.url = url;
    if([[PGLocalFileManager sharedManager]fileIsExist:url]){
        self.thumbView.image = [UIImage imageWithData:[[PGLocalFileManager sharedManager]fileWithURL:url]];
        if(self.thumbView.image==nil||[[PGLocalFileManager sharedManager] isJPEGValid:url]!=0){
            [[PGLocalFileManager sharedManager] removeFileWithURL:url];
        }
    }else{
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(imageLoaded:) name:NOTIFICATION_IMAGE_LOADED object:nil];
        [[PGArticleManager sharedManager]addQperator:[[PGHttpOperator alloc]initWithURLString:url cache:YES notification:NOTIFICATION_IMAGE_LOADED]];
    }
}
-(void)loadIcon:(NSString*)url{
    if([url componentsSeparatedByString:@"http://"].count<2){
        return;
    }
    self.iconUrl = url;
    if([[PGLocalFileManager sharedManager]fileIsExist:url]){
        self.iconView.image = [UIImage imageWithData:[[PGLocalFileManager sharedManager]fileWithURL:url]];
        NSLog(@"file exist!");
    }else{
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(iconLoaded:) name:NOTIFICATION_ICON_LOADED object:nil];
        [[PGArticleManager sharedManager]addQperator:[[PGHttpOperator alloc]initWithURLString:url cache:YES notification:NOTIFICATION_ICON_LOADED]];
    }
}
-(void)updateDate:(int)date{
    int time = [[NSDate date]timeIntervalSince1970]-date;
    if(time<3600*24){
        _dateLabel.text = @"最新";
    }else if(time<3600*48){
        _dateLabel.text = @"昨天";
    }else if(time<3600*72){
        _dateLabel.text = @"前天";
    }else{
        NSDate *currentDate = [NSDate dateWithTimeIntervalSince1970:date];
        NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:currentDate];
        _dateLabel.text = [NSString stringWithFormat:@"%.4d-%.2d-%.2d",components.year,components.month,components.day];
    }
}
-(void)imageLoaded:(NSNotification*)notification{
    if([[notification.userInfo objectForKey:@"url"]isEqualToString:self.url]){
        [[NSNotificationCenter defaultCenter]removeObserver:self name:NOTIFICATION_IMAGE_LOADED object:nil];
        if([[PGLocalFileManager sharedManager]fileIsExist:[notification.userInfo objectForKey:@"url"]]){
            self.thumbView.image = [UIImage imageWithData:[[PGLocalFileManager sharedManager]fileWithURL:[notification.userInfo objectForKey:@"url"]]];
        }else{
            if([[PGLocalFileManager sharedManager] saveFile:
                [notification.userInfo objectForKey:@"data"]
                                                withFileURL:[notification.userInfo objectForKey:@"url"]]){
            }
            self.thumbView.image = [UIImage imageWithData:[notification.userInfo objectForKey:@"data"]];
        }
    }
}
-(void)iconLoaded:(NSNotification*)notification{
    if([[notification.userInfo objectForKey:@"url"]isEqualToString:self.iconUrl]){
        [[NSNotificationCenter defaultCenter]removeObserver:self name:NOTIFICATION_ICON_LOADED object:nil];
        if([[PGLocalFileManager sharedManager]fileIsExist:[notification.userInfo objectForKey:@"url"]]){
            self.iconView.image = [UIImage imageWithData:[[PGLocalFileManager sharedManager]fileWithURL:[notification.userInfo objectForKey:@"url"]]];
            NSLog(@"file exist!");
        }else{
            if([[PGLocalFileManager sharedManager] saveFile:
                [notification.userInfo objectForKey:@"data"]
                                                withFileURL:[notification.userInfo objectForKey:@"url"]]){
                NSLog(@"file saved!");
            }
            self.iconView.image = [UIImage imageWithData:[notification.userInfo objectForKey:@"data"]];
        }
    }
}
@end
